/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uit_vendor_management;

import java.awt.Color;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author user
 */
public class PhieuXuatHangForm extends javax.swing.JFrame {

    /**
     * Creates new form PhieuXuatHangForm
     */
    boolean isMaDaiLyOK = false;
    boolean isMaMatHangOK = false;
    boolean isSoLuongOK = false;
    private Database myDatabase = Database.instance;
    
    public PhieuXuatHangForm() {
        initComponents();
        
        // init language
        this.initLanguage();
        
        // set time
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date();
        txbNgayLapPhieu.setText(dateFormat.format(date));
        
        // set focus
        this.Reload();
        
        lblError.setVisible(false);
        
        String ID = Database.getID("PHIEUXUATHANG", "SoPhieuXuat");
            txbMaPhieuXuat.setText(ID);
    }
    private void initLanguage()
    {
        lblPhieuXuatHang.setText(Language._("PHIEUXUATHANG_PHIEUXUATHANG_LBL"));
        lblMaPhieuXuat.setText(Language._("PHIEUXUATHANG_MAPHIEUXUAT_LBL"));
        lblMaDaiLy.setText(Language._("PHIEUXUATHANG_MADAILY_LBL"));
        lblMaMatHang.setText(Language._("PHIEUXUATHANG_MAMATHANG_LBL"));
        lblSoLuong.setText(Language._("PHIEUXUATHANG_SOLUONG_LBL"));
        lblNgayLapPhieu.setText(Language._("PHIEUXUATHANG_NGAYLAPPHIEU_LBL"));
        lblTen.setText(Language._("PHIEUXUATHANG_TEN_LBL"));
        lblLoaiDaiLy.setText(Language._("PHIEUXUATHANG_LOAIDAILY_LBL"));
        lblTenMatHang.setText(Language._("PHIEUXUATHANG_TENMATHANG_LBL"));
        btnLapPhieu.setText(Language._("PHIEUXUATHANG_LAPPHIEU_LBL"));
        btnXoaPhieu.setText(Language._("PHIEUXUATHANG_XUATPHIEU_LBL"));
        btnPhieuMoi.setText(Language._("PHIEUXUATHANG_PHIEUMOI_LBL"));
        btnThemMatHang.setText(Language._("PHIEUXUATHANG_THEMMATHANG_LBL"));
        btnTraCuu.setText(Language._("PHIEUXUATHANG_TRACUU_LBL"));
        btnThoat.setText(Language._("PHIEUXUATHANG_THOAT_LBL"));
   }
    
    private void Reload()
        {           
            QueryResult result =  myDatabase.Query("SELECT CHITIETPHIEUXUAT.MaChiTietPhieuXuat as STT, MATHANG.TenMatHang as 'Mặt Hàng', CHITIETPHIEUXUAT.DonViTinh as 'Đơn Vị Tính', CHITIETPHIEUXUAT.SoLuong as 'Số Lượng', CHITIETPHIEUXUAT.DonGia as 'Đơn Giá', CHITIETPHIEUXUAT.ThanhTien as 'Thành Tiền' FROM CHITIETPHIEUXUAT INNER JOIN MATHANG ON CHITIETPHIEUXUAT.MaMatHang = MATHANG.MaMatHang");
            if (result.ReturnCode == 200)
            {
                jTable.setModel(result.Table);
            }
        }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblPhieuXuatHang = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        lblMaPhieuXuat = new javax.swing.JLabel();
        lblMaDaiLy = new javax.swing.JLabel();
        lblMaMatHang = new javax.swing.JLabel();
        lblSoLuong = new javax.swing.JLabel();
        txbMaPhieuXuat = new javax.swing.JTextField();
        txbMaDaiLy = new javax.swing.JTextField();
        txbMaMatHang = new javax.swing.JTextField();
        txbSoLuong = new javax.swing.JTextField();
        lblNgayLapPhieu = new javax.swing.JLabel();
        txbNgayLapPhieu = new javax.swing.JTextField();
        txbTen = new javax.swing.JTextField();
        lblTen = new javax.swing.JLabel();
        lblLoaiDaiLy = new javax.swing.JLabel();
        txbLoaiDaiLy = new javax.swing.JTextField();
        txbTenMatHang = new javax.swing.JTextField();
        lblTenMatHang = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable = new javax.swing.JTable();
        btnLapPhieu = new javax.swing.JButton();
        btnXoaPhieu = new javax.swing.JButton();
        btnPhieuMoi = new javax.swing.JButton();
        btnThemMatHang = new javax.swing.JButton();
        btnTraCuu = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();
        lblError = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblPhieuXuatHang.setFont(new java.awt.Font("Calibri", 0, 48)); // NOI18N
        lblPhieuXuatHang.setText("PHIẾU XUẤT HÀNG");

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        lblMaPhieuXuat.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        lblMaPhieuXuat.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblMaPhieuXuat.setText("Mã Phiếu Xuất");

        lblMaDaiLy.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        lblMaDaiLy.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblMaDaiLy.setText("Mã Đại Lý");

        lblMaMatHang.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        lblMaMatHang.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblMaMatHang.setText("Mã Mặt Hàng");

        lblSoLuong.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        lblSoLuong.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblSoLuong.setText("Số lượng");

        txbMaPhieuXuat.setEditable(false);
        txbMaPhieuXuat.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N

        txbMaDaiLy.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        txbMaDaiLy.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txbMaDaiLyFocusLost(evt);
            }
        });
        txbMaDaiLy.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                txbMaDaiLyPropertyChange(evt);
            }
        });

        txbMaMatHang.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        txbMaMatHang.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txbMaMatHangFocusLost(evt);
            }
        });
        txbMaMatHang.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                txbMaMatHangInputMethodTextChanged(evt);
            }
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
        });

        txbSoLuong.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        txbSoLuong.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txbSoLuongFocusLost(evt);
            }
        });

        lblNgayLapPhieu.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        lblNgayLapPhieu.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblNgayLapPhieu.setText("Ngày Lập Phiếu");

        txbNgayLapPhieu.setEditable(false);
        txbNgayLapPhieu.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N

        txbTen.setEditable(false);
        txbTen.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N

        lblTen.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        lblTen.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblTen.setText("Tên");

        lblLoaiDaiLy.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        lblLoaiDaiLy.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblLoaiDaiLy.setText("Loại Đại Lý");

        txbLoaiDaiLy.setEditable(false);
        txbLoaiDaiLy.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N

        txbTenMatHang.setEditable(false);
        txbTenMatHang.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N

        lblTenMatHang.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        lblTenMatHang.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblTenMatHang.setText("Tên Mặt Hàng");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(lblMaMatHang, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txbMaMatHang, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(lblSoLuong, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txbSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(lblMaDaiLy, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblMaPhieuXuat, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE))
                        .addGap(20, 20, 20)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txbMaDaiLy)
                            .addComponent(txbMaPhieuXuat))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblTenMatHang, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txbTenMatHang, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(lblLoaiDaiLy, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txbLoaiDaiLy, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblNgayLapPhieu, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                            .addComponent(lblTen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txbTen, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txbNgayLapPhieu, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txbNgayLapPhieu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblNgayLapPhieu, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblTen, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txbTen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txbMaDaiLy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblMaDaiLy, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblLoaiDaiLy, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txbLoaiDaiLy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txbMaMatHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblMaMatHang, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblTenMatHang, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txbTenMatHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txbSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txbMaPhieuXuat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblMaPhieuXuat, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTable);

        btnLapPhieu.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        btnLapPhieu.setText("Lập phiếu");
        btnLapPhieu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLapPhieuActionPerformed(evt);
            }
        });

        btnXoaPhieu.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        btnXoaPhieu.setText("Xuất Phiếu");
        btnXoaPhieu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaPhieuActionPerformed(evt);
            }
        });

        btnPhieuMoi.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        btnPhieuMoi.setText("Phiếu Mới");
        btnPhieuMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPhieuMoiActionPerformed(evt);
            }
        });

        btnThemMatHang.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        btnThemMatHang.setText("Thêm Mặt Hàng");

        btnTraCuu.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        btnTraCuu.setText("Tra Cứu");

        btnThoat.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        lblError.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(28, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnLapPhieu)
                        .addGap(18, 18, 18)
                        .addComponent(btnXoaPhieu)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnPhieuMoi)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                        .addComponent(btnThemMatHang)
                        .addGap(18, 18, 18)
                        .addComponent(btnTraCuu)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnThoat))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(23, 23, 23))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(129, 129, 129)
                        .addComponent(lblPhieuXuatHang))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(152, 152, 152)
                        .addComponent(lblError)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(lblPhieuXuatHang, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(9, 9, 9)
                .addComponent(lblError)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnThemMatHang)
                        .addComponent(btnTraCuu)
                        .addComponent(btnThoat))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnLapPhieu)
                        .addComponent(btnXoaPhieu)
                        .addComponent(btnPhieuMoi)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnXoaPhieuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaPhieuActionPerformed
        // TODO add your handling code here:
        myDatabase.Query("DELETE FROM PHIEUXUATHANG WHERE SoPhieuXuat = '" + txbMaPhieuXuat.getText() + "'");
    }//GEN-LAST:event_btnXoaPhieuActionPerformed

    private void btnPhieuMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPhieuMoiActionPerformed
        // TODO add your handling code here:
        String ID = Database.getID("PHIEUXUATHANG", "SoPhieuXuat"); 
        txbMaPhieuXuat.setText(ID);
        
        // set Empty
        txbMaDaiLy.setText("");
        txbTen.setText("");
        txbMaMatHang.setText("");
        txbLoaiDaiLy.setText("");
        txbSoLuong.setText("");
        txbTenMatHang.setText("");
            
    }//GEN-LAST:event_btnPhieuMoiActionPerformed

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnThoatActionPerformed

    private void txbMaDaiLyPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_txbMaDaiLyPropertyChange
        // TODO add your handling code here:
        if (!"".equals(txbMaDaiLy.getText()))
            {
                if (Database.isDupplicate("DAILY", "MaDaiLy", txbMaDaiLy.getText()))
                {
                    // get TenDaiLy
                    QueryResult result = myDatabase.Query("SELECT * FROM DAILY WHERE MaDaiLy LIKE '" + txbMaDaiLy.getText() + "%'");
                    if (result.ReturnCode == 200)
                    {
                        DataTable table = result.Table;
                        txbTen.setText(table.getValueAt(0, 2).toString());
                        
                        // Get LoaiDaiLy
                        String maLoaiDaiLy = table.getValueAt(0, 1).toString();
                        result = myDatabase.Query("SELECT * FROM LOAIDAILY WHERE MaLoaiDaiLy = '" + maLoaiDaiLy + "'");
                        if (result.ReturnCode == 200)
                        {
                            txbLoaiDaiLy.setText(result.Table.getValueAt(0, 1).toString());
                        }
                        else
                        {
                            txbLoaiDaiLy.setText(" ");
                        }
                            
                    }
                    else
                    {
                        txbTen.setText("");
                        txbLoaiDaiLy.setText("");
                    }
                    
                }
                else
                {
                    txbTen.setText("");
                    txbLoaiDaiLy.setText("");
                }
            }
    }//GEN-LAST:event_txbMaDaiLyPropertyChange

    private void txbMaMatHangInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_txbMaMatHangInputMethodTextChanged
        // TODO add your handling code here:
        if (!"".equals(txbMaMatHang.getText()))
            {
                if (Database.isDupplicate("MATHANG", "MaMatHang", txbMaMatHang.getText()))
                {
                    QueryResult result = myDatabase.Query("SELECT * FROM MATHANG WHERE MaMatHang LIKE '" + txbMaMatHang.getText() + "%'");
                    if (result.ReturnCode == 200)
                    {
                        txbTenMatHang.setText(result.Table.getValueAt(0, 1).toString());
                    }
                    else
                    {
                        txbTenMatHang.setText("");
                    }
                }
            }
            else
            {
                txbTenMatHang.setText("");
            }
                
    }//GEN-LAST:event_txbMaMatHangInputMethodTextChanged

    private void btnLapPhieuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLapPhieuActionPerformed
        // TODO add your handling code here:
        // check
        this.txbMaDaiLyFocusLost(null);
        this.txbMaMatHangFocusLost(null);
        this.txbSoLuongFocusLost(null);
        
        boolean isEverythingOK = isMaDaiLyOK && isSoLuongOK && isMaMatHangOK;
        if (isEverythingOK)
        {
            if (!"".equals(txbTen.getText()) && !"".equals(txbTenMatHang.getText()))
            {
                Date date = new Date();
                String sqlString = "INSERT INTO PHIEUXUATHANG VALUES " +
                            "('" + txbMaPhieuXuat.getText() + "', '" + txbMaDaiLy.getText() + "', '" + Database.sqlDateTime((java.sql.Date) date) + "')";
                QueryResult result = myDatabase.Query(sqlString);
                if (result.ReturnCode == 200)
                {
                    sqlString = "SELECT * FROM QUYDINHMATHANG WHERE MaMatHang = '" + txbMaMatHang.getText() + "'";
                    result = myDatabase.Query(sqlString);
                    if (result.ReturnCode == 200)
                    {
                        String temp = result.Table.getValueAt(0, 2).toString();
                        int DonGia = Integer.parseInt(result.Table.getValueAt(0, 3).toString());
                        int SoLuong = Integer.parseInt(txbSoLuong.getText());
                        int ThanhTien = DonGia * SoLuong;
                        
                        String ID = Database.getID("CHITIETPHIEUXUAT", "MaChiTietPhieuXuat");
                        sqlString = "INSERT INTO CHITIETPHIEUXUAT VALUES " +
                                "('" + ID + "', '" + txbMaMatHang.getText() 
                                + "', '" + txbMaPhieuXuat.getText() + "', '" + SoLuong 
                                + "', '" + DonGia + "', '" + temp + "', '" + ThanhTien + "')";
                        myDatabase.Query(sqlString);
                        
                        this.Reload();
                    }
                }
            }
        
        
        }
    }//GEN-LAST:event_btnLapPhieuActionPerformed

    private void txbMaDaiLyFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txbMaDaiLyFocusLost
        // TODO add your handling code here:
        boolean isHasError = false;
        if ("".equals(txbMaDaiLy.getText()))
        {
            isHasError = true;
        }
        
        if (isHasError)
        {
            isMaDaiLyOK = false;
            
            txbMaDaiLy.setBackground(Color.red);
            lblError.setVisible(true);
            lblError.setText(Language._("ERROR_MADAILY_NULL"));
        }
        else
        {
            isMaDaiLyOK = true;
            txbMaDaiLy.setBackground(Color.white);
            lblError.setVisible(false);
        }
    }//GEN-LAST:event_txbMaDaiLyFocusLost

    private void txbMaMatHangFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txbMaMatHangFocusLost
        // TODO add your handling code here:
        boolean isHasError = false;
        if ("".equals(txbMaMatHang.getText()))
        {
            isHasError = true;
        }
        
        if (isHasError)
        {
            isMaMatHangOK = false;
            txbMaMatHang.setBackground(Color.red);
            lblError.setVisible(true);
            lblError.setText(Language._("ERROR_MAMATHANG_NULL"));
        }
        else
        {
            isMaMatHangOK = true;
            txbMaMatHang.setBackground(Color.white);
            lblError.setVisible(false);
        }
    }//GEN-LAST:event_txbMaMatHangFocusLost

    private void txbSoLuongFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txbSoLuongFocusLost
        // TODO add your handling code here:
        boolean isHasError = false;
        if ("".equals(txbSoLuong.getText()) )
        {
            isHasError = true;
        }
        try
        {
            int soLuong;
            soLuong = Integer.parseInt(txbSoLuong.getText());
        }
        catch (NumberFormatException e)
        {
            isHasError = true;
        }

        if (isHasError)
        {
            isSoLuongOK = false;
            txbSoLuong.setBackground(Color.red);
            lblError.setVisible(true);
            lblError.setText(Language._("ERROR_SOLUONG_NULL"));
        }
        else
        {
            isSoLuongOK = false;
            txbSoLuong.setBackground(Color.white);
            lblError.setVisible(false);
        }
    }//GEN-LAST:event_txbSoLuongFocusLost

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PhieuXuatHangForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PhieuXuatHangForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PhieuXuatHangForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PhieuXuatHangForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PhieuXuatHangForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLapPhieu;
    private javax.swing.JButton btnPhieuMoi;
    private javax.swing.JButton btnThemMatHang;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnTraCuu;
    private javax.swing.JButton btnXoaPhieu;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable;
    private javax.swing.JLabel lblError;
    private javax.swing.JLabel lblLoaiDaiLy;
    private javax.swing.JLabel lblMaDaiLy;
    private javax.swing.JLabel lblMaMatHang;
    private javax.swing.JLabel lblMaPhieuXuat;
    private javax.swing.JLabel lblNgayLapPhieu;
    private javax.swing.JLabel lblPhieuXuatHang;
    private javax.swing.JLabel lblSoLuong;
    private javax.swing.JLabel lblTen;
    private javax.swing.JLabel lblTenMatHang;
    private javax.swing.JTextField txbLoaiDaiLy;
    private javax.swing.JTextField txbMaDaiLy;
    private javax.swing.JTextField txbMaMatHang;
    private javax.swing.JTextField txbMaPhieuXuat;
    private javax.swing.JTextField txbNgayLapPhieu;
    private javax.swing.JTextField txbSoLuong;
    private javax.swing.JTextField txbTen;
    private javax.swing.JTextField txbTenMatHang;
    // End of variables declaration//GEN-END:variables
}
